package al.softsynergy.auk.ui.ankesa;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Panel;
import al.softsynergy.auk.data.containers.AnkesaContainer;
import al.softsynergy.auk.data.ejbs.IAukBean;
import al.softsynergy.auk.data.entries.AnkesaLloj;
import al.softsynergy.auk.ui.MainWindow;


public class AnkesaUI  implements ValueChangeListener, ItemClickListener{

	
	private AnkesaNavigationTree ankesaNavigationTree = new AnkesaNavigationTree(this);
	private AnkesaContainer dataSource;
	private BeanItemContainer<AnkesaLloj> ankesaLlojContainer;
	
	private AnkesaList ankesaList;
	private AnkesaFormPanel ankesaFormPanel;
	private AnkesaSearchPanel ankesaSearchPanel;
	
	
	
	private MainWindow mainWindow;
	
	
	public AnkesaUI(MainWindow mainWindow) {
		super();
		this.mainWindow = mainWindow;
		
		this.dataSource = AnkesaContainer.createFromEjb(this.getAukBean());
		this.ankesaLlojContainer = new BeanItemContainer<AnkesaLloj>(this.getAukBean().queryAnkesaLloj());
		this.showAnkesaNavigation();
		this.showAnkesaList();
			
	}

	
	
	public AnkesaContainer getDataSource(){
		return this.dataSource;
	}
	
	public void showAnkesaNavigation(){
		this.mainWindow.loadSideBar(this.ankesaNavigationTree);
	}
	public void showAnkesaList(){
		this.ankesaList = null;
		if (ankesaList == null) this.ankesaList = new AnkesaList(this);
		this.mainWindow.loadContent(this.ankesaList);
		this.mainWindow.loadContentTitle("Lista e ankesave");
		this.mainWindow.loadSubContentTitle("");
		this.ankesaNavigationTree.select(AnkesaNavigationTree.LIST);
		
	}

	public void showAnkesaForm(){

		if (ankesaFormPanel == null) 
			this.ankesaFormPanel = new AnkesaFormPanel(this);
		
		this.mainWindow.loadContent(this.ankesaFormPanel);
		this.mainWindow.loadContentTitle("Shtimi/ndryshimi i nje ankese");
		this.mainWindow.loadSubContentTitle("");
		this.ankesaNavigationTree.select(AnkesaNavigationTree.ADD);
		
	}
	
	public void showAnkesaSearch(){
		if (ankesaSearchPanel == null) this.ankesaSearchPanel = new AnkesaSearchPanel(this);
		this.mainWindow.loadContent(this.ankesaSearchPanel);
		this.mainWindow.loadContentTitle("Kerko ankese");
		this.mainWindow.loadSubContentTitle("Duhet qe te jepet te pakten njera nga fushat");
		this.ankesaSearchPanel.reset();
	}
	
	
	public IAukBean getAukBean() {
		return this.mainWindow.getAukBean();
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
		if (property == this.ankesaList){
			Item item = ankesaList.getItem(ankesaList.getValue());
			this.showAnkesaForm();
			if (   item != ankesaFormPanel.getAnkesaForm().getItemDataSource()){
				ankesaFormPanel.getAnkesaForm().setItemDataSource(item);
			}
			
		}
		
	}



	@Override
	public void itemClick(ItemClickEvent event) {
		if (event.getSource() == this.ankesaNavigationTree){
			Object itemId = event.getItemId();
			if (itemId != null) {
				if (AnkesaNavigationTree.LIST.equals(itemId)){
					this.dataSource = AnkesaContainer.createFromEjb(this.getAukBean());
					this.showAnkesaList();
					
					
				}else if (AnkesaNavigationTree.ADD.equals(itemId)){
					
					this.showAnkesaForm();
					this.ankesaFormPanel.getAnkesaForm().addAnkese();
				}
				else if (AnkesaNavigationTree.SEARCH.equals(itemId)){
					this.showAnkesaSearch();
					
				}
				
			}
		}
		
	}

	public BeanItemContainer<AnkesaLloj> getAnkesaLlojContainer() {
		return ankesaLlojContainer;
	}



	public void setDataSource(AnkesaContainer dataSource) {
		this.dataSource = dataSource;
	}
	
}
